#include "charging_zhongsheng_io.h"
#include "utility.h"
chargingZhongSheng_IO::chargingZhongSheng_IO( QObject* parent ) {
    qDebug() << "chargingZhongSheng_IO";
}

chargingZhongSheng_IO::~chargingZhongSheng_IO() {}

bool chargingZhongSheng_IO::chargingOpen() {
    qDebug() << "chargingZhongSheng_IO:chargingOpen";
    outAction = OPEN;
    if ( isConnected() ) {
        bool outback = setOut( open_data, 2 );
        if ( !outback ) { emit sigSendTips( "充电桩伸出指令错误" ); }
        else {
            emit sigSendTips( "伸出动作" );
            t->start( 2000 );
        }
        return outback;
    }
    else {
        qDebug() << "未连接！";
        return false;
    }
}

bool chargingZhongSheng_IO::chargingClose() {
    outAction = CLOSE;
    if ( isConnected() ) {
        bool outback = setOut( close_data, 2 );
        if ( !outback ) { emit sigSendTips( "充电桩收回指令错误" ); }
        else {
            emit sigSendTips( "收回动作" );
            t->start( 2000 );
        }
        return outback;
    }
    else {
        return false;
    }
}

bool chargingZhongSheng_IO::chargingStop() {
    outAction = STOP;
    if ( isConnected() ) {
        bool outback = setOut( close_all_data, 2 );
        if ( !outback ) { emit sigSendTips( "充电桩停止指令错误" ); }
        else {
            emit sigSendTips( "停止动作" );
            //停止后，一律关闭通讯
            QTimer::singleShot( 1000, this, [&]() { tcpClose(); } );
            return outback;
        }
    }
    else {
        return false;
    }
}

void chargingZhongSheng_IO::onTcpReadyRead() {
    if ( _qsocket->bytesAvailable() <= 0 ) { qDebug() << "ready 0 byte"; }
    QByteArray feedback = _qsocket->readAll();
    sigShowTCPData( feedback, QString( "Recv: [%1:%2] [读输入] " ).arg( _qsocket->peerName() ).arg( _qsocket->peerPort() ) );
    qDebug() << "recv data :" << feedback << " data size:" << feedback.size();
    if ( feedback.size() == 13 ) {
        //输入，输出状态
        if ( feedback.at( 1 ) == '\x04' ) {
            limits.front = feedback.at( 4 ) == 1;
            limits.back  = feedback.at( 6 ) == 1;
            qDebug() << "前限位感应" << limits.front;
            qDebug() << "后限位感应" << limits.back;
        }
        qDebug() << ( limits.front || limits.back ) << isFinish << outAction;
        if ( limits.front && outAction == OPEN ) {
            qDebug() << "完成伸出";
            emit sigSendTips( "完成伸出" );
            isFinish = true;
        }
        if ( limits.back && outAction == CLOSE ) {
            qDebug() << "完成收回";
            emit sigSendTips( "完成收回" );
            isFinish = true;
        }
        if ( ( limits.front && isFinish ) || ( limits.back && isFinish ) ) {
            chargingStop();
            emit sigFinish();
        }
    }
}

void chargingZhongSheng_IO::sltTimeOut() {
    qDebug() << "chargingZhongSheng_IO: sltTimeOut";
    setOut( state_in, 1 );
}

bool chargingZhongSheng_IO::setOut( uint8_t data[], int type ) {

    if ( type == 1 ) {
        //读取输入，输出通道
        QByteArray array( ( char* )data, 9 );
        qDebug() << "chargingZhongSheng_IOsend data" << array;
        _qsocket->write( array, 9 );
        return true;
    }
    else {
        //输出控制
        QByteArray array( ( char* )data, 15 );
        qDebug() << "chargingZhongSheng_IOsend data" << array;
        //        emit       sigSendTips( array );
        _qsocket->write( array, 15 );
        return true;
    }
}
